<div id="wrapMain">
            <div class="wrap_1024" style="margin-bottom:40px">

            <div class='head_nav'><span class="icon-picture"></span> Get story chapters</div>
            <div class="user_profile">
				
				<div class="row" >
					<div class="col-md-12 ">
						<div id="contentIndex" class="col-md-12">
										<div role="dialog" id="modalAlertBox" class="modal fade" style="display: none;">
											<div class="modal-dialog modal-wide">
												<div class="modal-content">
													<div class="modal-header">
														<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
														<h4 class="modal-title"></h4>
													</div>
											        <div class="modal-body">
														
														<div style="clear:both"></div>
											        </div>
												</div><!-- /.modal-content -->
											</div>
										</div>
										
										<div id="wrapLoadMoreItems">
											<div class="normalLoadBtn" id="loadMoreItems"><img width="20" alt="loading" src="public/images/loading.gif"> Loading </div><br>
										</div>
										<div class="formLoading"></div>
										
									    <div class="generalError" style="display: none;"></div>
									    
									    <form id="videoUpload" class="userPostForm" role="form" method="post" enctype="multipart/form-data">
											 <div class="form-group">
												<div class="row">
	                                                <div class="col-md-12">
	                                                Tổng cộng: <?php echo $data['total_truyen']; ?> truyện.<br />
	                                                Lấy: <?php echo $data['limit_stories']; ?> truyện <br />
	                                                
														<table  class="story_table" id="report_chapter_table">
															<thead>
																<tr>
																	<th align="center">Stt</th>
																	<th align="center">Tên truyện</th>
																	<th align="center">Link</th>
																</tr>
															</thead>
															<tbody>
																
																<?php 
																	if($data['stories']):
																	$i = 1;
																	foreach($data['stories'] as $value):
																	?>
																	<tr>
																	<td><?= $i ?></td>
																	<td><a href="<?= BASE_URL . 'index.php?module=user&act=new_getstorychapt' ?>#truyen<?= $value['id']; ?>"><?= $value['ten']; ?></a></td>
																	<td><?= $value['getlink_source']; ?></td>
																		</tr>
																	<?php
																	$i++;
																	?>
																
																	<?php 
																	endforeach;
																endif;
																?>
															</tbody>
														</table>
													</div>
                                                </div>
                                            </div>
										
											<br />
											
                                            <div class="row"> <!-- Start class row wrap upload image and submit button -->
                                            <div class="col-md-2">
                                            </div>
                                            <div class="col-md-10">
											<button type="submit" class="btn btn-primary mySubmit" id="submitSplit">
							                    <i class="glyphicon glyphicon-hand-up"></i>
							                    <span>Đăng bài</span>
							                </button>
                                            </div> <!-- End of class col-md-10 wrap upload image and submit button -->
                                            </div><!-- End of class row wrap upload image and submit button -->
											
										</form>
										<ul class="nav nav-tabs">
  <li><a href="#chapterlink" data-toggle="tab">Chapter links</a></li>
  <li><a href="#chaptercontent" data-toggle="tab">Chapter Content</a></li>
</ul>
<div class="tab-content">
  <div class="tab-pane active" id="chapterlink">
  	<table  class="story_table" id="report_chapter_link_table">
		<tbody>
		</tbody>
	</table>
  </div>
  <div class="tab-pane" id="chaptercontent">
  	<table  class="story_table" id="report_chapter_content_table">
		<thead>
			<tr>
				<th>Truyện id</th>
				<th>Tên truyện</th>
				<th>Truyện chương link id</th>
				<th>Thứ tự</th>
			</tr>
		</thead>
		<tbody>
			
		</tbody>
	</table>
  </div>
</div>
										
				
						</div><!-- end #contentIndex -->
					</div>
					
					
				</div> <!-- End of row -->
			</div><!-- end wrap_1024 -->
		</div><!-- end wrapMain -->
		
<!-- end #wrapMain -->		
		

<script type="text/javascript">
	$(document).ready(function(){
		var splitLimit = <?php echo LIMIT_SPLIT ?>;
	    /*
	     * Submit event
	     */
	    $('button#submitSplit').click(function(e){
		 
	    	e.preventDefault();
	    	$(".alert").remove();
	  		$('.userPostForm').css('opacity', '0.3');
			$('.formLoading')
			.css('width', $('#videoUpload').width())
			.css('height', $('#videoUpload').height())
			.css('opacity', '1')
			.css('z-index','9999').css('display', 'block');
			$('#wrapLoadMoreItems').css('display', 'block').center();

			
			var time = 0;
			
			splitTable(time);
	    });

	    function splitTable(time){
		    
	    	$.ajax({
				// &manual=1 : Lấy những truyện chưa submit lần nào
				url: "<?php echo BASE_URL; ?>index.php?module=user&act=ajax&startSplit=1",
				type : 'POST',
				cache : false,
				async: false,
				data :{splitLimit: splitLimit, time: time},

				success: function(result){
					
					if(result == 'het'){
						return;
					}

					var newtime = parseInt(time) + 1;
		    		splitTable(newtime);
				}
				
			});

	    }

	   	function getChaptersViaPage(newData){
		   	storyType = newData.getlink_type;
		   	startPage = newData.startPage;
	   		newurl = newData.url.replace('%s%', startPage);
	   		
	   		returnSend = false;
	   		
	   		$.ajax({
				url: "<?php echo HOME; ?>index.php?module=user&act=ajax&validateGetChapter=1",
				type: 'POST',
				cache: false,
				//dataType: 'jsonp',
				async: false,
				timeout: 800000,
				data: {txtSource: storyType, txtSourceUrl: newurl, truyenId : newData.id},
				success: function(data1){
					if(data1 != 'het'){
						
						//console.log('here if khac false - ' + data1);
						if(data1) returnData = $.parseJSON(data1);
						$.ajax({
							url: "<?php echo HOME; ?>index.php?module=user&act=ajax&insertChapter=1",
							type: 'POST',
							cache: false,
							async: false,
							data: {links: data1, storyData: newData},
							success: function(insertData){
								i = 1;
								$.each(returnData, function(e,index){
									if(index.title == 'Error'){
										if(i%2==0){trclass = 'class="odd"';}else{trclass='class="even"';}
										if(index.href == 'Error') {status = '<td class="fail_story">Fail</td>';}else{status = '<td>OK</td>'}
										$('#report_chapter_link_table tbody').append('<tr '+trclass+'><td>'+i+'</td><td>'+index.link+'</td><td>' + index.title + '</td><td>'+newData.startPage+'</td><td>'+index.tap+'</td><td>'+index.chapt+'</td>'+status);
									}
									i++;
								});
							},
						});
						
					}else{
						//console.log('here returnSend true');
						returnSend = true;
						//return false;
					}
				},

				error: function(x, t, m) {
			        if(t==="timeout") {
			            alert("got timeout");
			        } else {
				        console.log(x);
				        console.log(t);
				        console.log(m);
			        	
			        }
			    },
				
				complete: function(jqXHR, textStatus){
					
					if(returnSend == true){
						console.log('returnSend ' + returnSend);
						return;
					}

					newData.startPage = parseInt(newData.startPage) + 1;
					getChaptersViaPage(newData);
				}
			});
	   	}

	   	

	   	function updateStoryManualStatus(storyId){
	   		$.ajax({
	    		url : "<?php echo BASE_URL; ?>index.php?module=user&act=ajax&updateStoryManualStatus=1",
	    		type : 'POST',
	    		cache: false,
	    		data: {story_id:storyId},
	    		success: function(data){
					return;
		    	}
	   		});
	   	}
	    
	    $('.select2').select2({ width: '250' });
		$('#txtSource').select2({width: '150'});
	});
</script>

<script src="public/exts/js/jquery.center.js"></script>
